<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="免费停车/小时" prop="freeTime">
        <el-input v-model="queryParams.freeTime" placeholder="请输入免费停车/小时" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="基础单价/元" prop="basePrice">
        <el-input v-model="queryParams.basePrice" placeholder="请输入基础单价" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="标准单价/元" prop="price">
        <el-input v-model="queryParams.price" placeholder="请输入标准单价" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="rulesStatus">
        <el-select v-model="queryParams.rulesStatus" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.parkingcar_rules_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['parkingcar:rules:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['parkingcar:rules:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['parkingcar:rules:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['parkingcar:rules:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rulesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="规则ID" align="center" prop="rulesId" />
      <el-table-column label="单日封顶金额" align="center" prop="maxPrice" />
      <el-table-column label="免费停车/小时" align="center" prop="freeTime" />
      <el-table-column label="基础单价/元" align="center" prop="basePrice" />
      <el-table-column label="基础单价/小时" align="center" prop="baseTime" />
      <el-table-column label="标准单价/元" align="center" prop="price" />
      <!-- <el-table-column label="状态" align="center" prop="rulesStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.parkingcar_rules_status" :value="scope.row.rulesStatus" />
        </template>
      </el-table-column> -->
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.rulesStatus"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      
      <el-table-column label="描述" align="center" prop="node" width="300">
        <template slot-scope="scope">
          <el-button type="text" @click="openMessage2(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['parkingcar:rules:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['parkingcar:rules:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改收费规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单日封顶金额" prop="maxPrice">
          <el-input v-model="form.maxPrice" placeholder="请输入单日封顶金额" />
        </el-form-item>
        <el-form-item label="免费停车/小时" prop="freeTime">
          <el-input v-model="form.freeTime" placeholder="请输入免费停车/小时" />
        </el-form-item>
        <el-form-item label="基础单价/元" prop="basePrice">
          <el-input v-model="form.basePrice" placeholder="请输入基础单价" />
        </el-form-item>
        <el-form-item label="基础单价/小时" prop="baseTime">
          <el-input v-model="form.baseTime" placeholder="请输入基础单价/时间" />
        </el-form-item>
        <el-form-item label="标准单价/元" prop="price">
          <el-input v-model="form.price" placeholder="请输入标准单价" />
        </el-form-item>
        <el-form-item label="状态" prop="rulesStatus">
          <el-select v-model="form.rulesStatus" placeholder="请选择状态">
            <el-option v-for="dict in dict.type.parkingcar_rules_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="node">
          <el-input v-model="form.node" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRules, getRules, delRules, addRules, updateRules } from "@/api/parkingcar/rules";

export default {
  name: "Rules",
  dicts: ['parkingcar_rules_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收费规则表格数据
      rulesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        freeTime: null,
        basePrice: null,
        price: null,
        rulesStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        maxPrice: [
          { required: true, message: "单日封顶金额不能为空", trigger: "blur" }
        ],
        basePrice: [
          { required: true, message: "基础单价不能为空", trigger: "blur" }
        ],
        baseTime: [
          { required: true, message: "基础单价/时间不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "标准单价不能为空", trigger: "blur" }
        ],
        rulesStatus: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        node: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ]
      },

      messageContent: "", // 描述信息
      messageBoxTitle: "描述信息" // 弹出框标题
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询收费规则列表 */
    getList() {
      this.loading = true;
      listRules(this.queryParams).then(response => {
        this.rulesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rulesId: null,
        maxPrice: null,
        freeTime: null,
        basePrice: null,
        baseTime: null,
        price: null,
        rulesStatus: null,
        node: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rulesId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收费规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rulesId = row.rulesId || this.ids
      getRules(rulesId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收费规则";
      });
    },

    //描述弹窗
    openMessage2(row) {
      console.log(row)
      this.messageContent = row.node;
      
      this.messageBoxTitle = "收费规则"; // 设置弹出框标题
      // this.open = true; 
      this.$alert(this.messageContent, this.messageBoxTitle, {
        confirmButtonText: '确定',
        callback: action => {
          this.$message({
            type: 'info',
            message: `action: ${action}`
          })
        }

      })
    },

    // 收费规则状态修改
    handleStatusChange(row) {
      let text = row.rulesStatus == "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text   + '"该收费方案吗？').then(function () {
        const data = {
          rulesId: row.rulesId,
          rulesStatus: row.rulesStatus

        }
        return updateRules(data);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.rulesStatus = row.rulesStatus === 0 ? 1 : 0;
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rulesId != null) {
            updateRules(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRules(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rulesIds = row.rulesId || this.ids;
      this.$modal.confirm('是否确认删除收费规则编号为"' + rulesIds + '"的数据项？').then(function () {
        return delRules(rulesIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('parkingcar/rules/export', {
        ...this.queryParams
      }, `rules_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
